var linkMoveLeft = 0, linkMoveTop = 0;
var linkLeft = null, linkRight = null, linkTop = null, linkButtom = null;
var linkWidth = 2;
var linkColor = null;
var changeNodePosition = function () { };
var spouseLinkList = {};
//链接线
var links = function () {
    resetlinkCnt = 0;
    resetPosCnt = 0;
    spouseJoinCnt = 0;
    linkMoveLeft = 0;
    linkMoveTop = 0;
    linkLeft = null, linkRight = null, linkTop = null, linkButtom = null;

 
    spouseLinkList = {}, nodePositions = {};

    //处理 直线
    canvas = document.getElementById("treeCanvas");
    if (canvas.getContext) {
        canvas.style.position = 'absolute';
        canvas.width = (jpMaxX - jpMinX) + 400;
        canvas.height = (jpMaxY - jpMinY) + 200;
        canvas.style.top = '0px';
        canvas.style.left = '0px';
        drawline = canvas.getContext('2d');
        drawline.linkWidth = linkWidth;
        drawline.globalAlpha = 1;   //这里采用简单算法布置透明     
        drawline.strokeStyle =linkColor ?linkColor: "#a7cbca";

    }
    for (var id in $nodes) {
        addNodes($nodes[id], db2treeNode($nodes[id]));
    }
    changeNodePosition();
    console.log('time go spouse ' + Date().toString());
    getSpouseList();
    console.log('time' + Date().toString());
    linkParents();
}
var AfterMoveLink = function (allw, allh) {
    $cont = $("#cont-body");
    var zengkuan = ($cont.width() + 600) / currentScale;
    var sw, sh;
    if (allw) {
        sw = allw;
    }
    else {
        sw = $(window).width();
    }
    if (allh) {
        sh = allh;
    }
    else {
        sh = $(window).height();
    }
    sw = sw + zengkuan;
    sh = sh + zengkuan;
    canvas = document.getElementById("treeCanvas");
    canvas.height = sh;
    canvas.width = sw;
    drawline = canvas.getContext('2d');
    drawline.linkWidth = linkWidth;
    $group = $("#translate-group");
    var gtop = $group.position().top / currentScale;
    var gleft = $group.position().left / currentScale;
    drawline.beginPath();
    drawline.moveTo(0, 0);
    $gh = $group.height();
    $gw = $group.width();
    var ctop = -(gtop + zengkuan / 2);
    var cleft = -(gleft + zengkuan / 2);
    linkLeft = cleft, linkRight = cleft + sw, linkTop = ctop, linkButtom = ctop + sh;
    canvas.style.top = ctop + "px"; // '0px';
    canvas.style.left = cleft + "px"; // '0px';
    spouseListJoin();
    linkParents();
}


//子女自检链接
var spouseListJoin = function () {
    for (var k in spouseLinkList) {
        spouseLinkList[k].link();
    }
}

//获得夫妻间的 位置关系
var getSpouseList = function () { 
    for (var j = 0; j < jpnodes.length; j++) {
        var n = jpnodes[j];
        if (n.isDelete == 1 || n.isShow == 0) {
            continue;
        }
        if (n.spouse.length > 0) {
            for (var m = 0; m < n.spouse.length; m++) {
                var s = n.spouse[m];
                var sj = new SpouseJoin(n, s);
                spouseLinkList[sj.Key] = sj;
                sj.link();
            }
        } else {
            var sj = new SpouseJoin(n, null);
            spouseLinkList[sj.Key] = sj;
        }
    }
  
}
var goMove = false;
var linkParents = function () {
    for (var j = 0; j < jpnodes.length; j++) {

        if (goMove) {
            goMove = false;
            return;
        }
        var n = jpnodes[j];
        if (n.isDelete == 1 || n.isShow == 0) {
            continue;
        }
        var f = n.father;
        var m = n.mother;
        if (!m && !f) {
            continue;
        }
        var key;
        if (f && m && !((sxSetting.spousejoin == 1) && isShiXiTu)) {
            key = f.id + "_" + m.id;
        }
        else if (m && !(( sxSetting.spousejoin==1)&& isShiXiTu)) {
            key = m.id + "_";
        }
        else if (f) {
            key = f.id + "_";
        }
        var spouseJoin = spouseLinkList[key];
        if (spouseJoin && spouseJoin.Enable) {
            var np = new NodePosition(n);
            if (np.Enable) {
               
                drawline.strokeStyle = spouseJoin.LinkColor();
                spouseJoin.link();
                var npTop = np.Top.y;
                var npLeft = np.Top.x; // npLeft.y;
                var y = np.Top.y - lineSplit * (spouseJoin.Sort + 2);
                if (sxSetting.jiantou == 1 && isShiXiTu) {
                    y -= 20;
                }
                var xCenter = spouseJoin.JoinCenter.x;
                var yCenter = spouseJoin.JoinCenter.y;
                if (linkLeft !== null) {
                    if ((yCenter < linkTop || yCenter > linkButtom) && (xCenter < linkTop || xCenter > linkButtom)) {
                        continue;
                    }
                    npTop -= linkTop;
                    npLeft -= linkLeft;
                    xCenter -= linkLeft;
                    yCenter -= linkTop;
                    y -= linkTop;
                }
                drawline.beginPath();
                drawline.moveTo(npLeft, npTop);
                drawline.lineTo(npLeft, y);
                drawline.lineTo(xCenter, y);
                drawline.lineTo(xCenter, yCenter);


                if (sxSetting.jiantou == 1 && isShiXiTu) {
                    drawline.moveTo(npLeft - 20, npTop - 20);
                    drawline.lineTo(npLeft, npTop);
                    drawline.lineTo(npLeft + 20, npTop - 20);
                }
                drawline.stroke();
            }
        }
    }
}



//节点链接
var SpouseJoin = function (nodeX, nodeY) {

    this.Nan;
    this.Nv;
    this.NanP;
    this.NvP;
    this.Enable = true;
    this.Sort = 0;
    this.Key = ""; //Key
    this.Linked = 0; //是否已经链接
    this.LinkColor = function () {
        if (isShiXiTu) {
            return "#000";
        }
        //return "#000";
        if (linkColor != null) {
            return linkColor;
        }
        if (this.Sort <= 1) {
            return "#a7cbca";
        }
        if (this.Sort == 2) {
            return "#dfa296";
        }
        if (this.Sort == 3) {
            return "#009a61";
        }

        return "#f50";
    }
    //链接的中心点
    this.JoinCenter = new Posistion();

    this.reset = function (x, y) {
        spouseJoinCnt++;
        if (x && y) {
            if (x.sex == 1) {
                if (x.spouse.indexOf(y) < 0) {
                    this.Enable = false;
                    return;
                }
                this.Nan = x;
                this.Nv = y;
            }
            else {
                if (y.spouse.indexOf(x) < 0) {
                    this.Enable = false;
                    return;
                }
                this.Nan = y;
                this.Nv = x;
            }
        }
        else if (x) {
            this.Nan = x;
        }
        else if (y) {
            this.Nan = y;
        }
        else {
            this.Enable = false;
            return;
        }
        if (( sxSetting.spousejoin == 1) && isShiXiTu) {
            this.Nv = null;
        }

        if (this.Nv) {
            var nan = new NodePosition(this.Nan);
            this.NanP = nan;
            var nv = new NodePosition(this.Nv);
            this.NvP = nv;
            this.Sort = this.Nan.spouse.indexOf(this.Nv) + 1;
            var p = new Posistion();
            p.x = (nan.Bottom.x + nv.Bottom.x) / 2;
            p.y = nan.Bottom.y + this.Sort * lineSplit;
            //添加 一个 左右点的 大小
            var pdd = 0;
            if (this.Nan.id) {
                var n2 = sxSetting.spousejoin == 0  ? nodeLeftHeightAppMax[this.Nan.id] : nodeRightHeightAppMax[this.Nan.id];
                if (n2) {
                    pdd = n2;
                }
            }
            n2 = sxSetting.spousejoin == 0 ? nodeRightHeightAppMax[this.Nv.id] : nodeLeftHeightAppMax[this.Nv.id];
            if (n2 && n2 > pdd) {
                pdd = n2;
            }
            p.y += pdd;
            this.JoinCenter = p;
            this.Key = this.Nan.id + "_" + this.Nv.id;
        }
        else {
            var nan = new NodePosition(this.Nan);
            this.NanP = nan;
            var p = new Posistion();
            this.Sort = 1;
            p.x = nan.Bottom.x;
            p.y = nan.Bottom.y + this.Sort * lineSplit;
            //添加 一个 左右点的 大小
            var n2 = sxSetting.spousejoin == 0 ? nodeLeftHeightAppMax[this.Nan.id] : nodeRightHeightAppMax[this.Nan.id];
            var pdd = 0;
            if (n2) {
                pdd = n2;
            }
            p.y += pdd;
            this.JoinCenter = p;
            this.Key = this.Nan.id + "_";
        }

        this.Linked = 0;
    }
    this.reset(nodeX, nodeY);
    this.reset = function () {
        this.reset(this.Nan, this.Nv);
    }

    this.link = function () {
        if (!this.Linked) {
            for (var start = 0; start < 3; start++) {
                drawline.strokeStyle = this.LinkColor();
                drawline.beginPath();
                var npbx = this.NanP.Bottom.x;
                var npby = this.NanP.Bottom.y;
                var centerX = this.JoinCenter.x;
                var centerY = this.JoinCenter.y;

                if (linkLeft !== null) {
                    if ((centerY < linkTop || centerY > linkButtom) && (centerX < linkTop || centerX > linkButtom)) {
                        return;
                    }
                    npbx -= linkLeft;
                    npby -= linkTop;
                    centerX -= linkLeft;
                    centerY -= linkTop;
                }
                drawline.moveTo(npbx, npby);
                drawline.lineTo(npbx, centerY);
                drawline.lineTo(centerX, centerY);
                drawline.stroke();
                //关闭绘制路径
                drawline.closePath();
                if (this.Nv) {
                    drawline.beginPath();
                    var npbx = this.NvP.Bottom.x;
                    var npby = this.NvP.Bottom.y;
                    if (linkLeft !== null) {
                        npbx -= linkLeft;
                        npby -= linkTop;
                    }
                    drawline.moveTo(npbx, npby);
                    drawline.lineTo(npbx, centerY);
                    drawline.lineTo(centerX, centerY);
                    drawline.stroke();
                    //关闭绘制路径
                    drawline.closePath();
                }
            }
        }
    }
}


//位置节点
var Posistion = function () {
    this.x = 0;
    this.y = 0;
    this.enable = true;
};

//节点的位置集合
var NodePosition = function (node) {
    if (nodePositions[node.id]) {
        return nodePositions[node.id];
    }
    this.Top;
    this.Bottom;
    this.Enable = true;
    this.Node;
    //初始化
    this.init = function (node) {
        this.Node = node;
        this.reset();
    }
    //重新获得位置
    this.reset = function () {
        if (this.Node.isDeleted || !this.Node.isShow) {
            this.Enable = false;
            return;
        }
        var l = 0, t = 0;

        var l = node.x;
        var t = node.y;
      
        var p = new Posistion();
        p.x = l + nwidth / 2;
        p.y = t;
        this.Top = p;

        p = new Posistion();
        p.x = l + nwidth / 2;
        p.y = t + nheight + bottomMargin * canvasScaleOp;
        //进行加和
        var h = nodeBottomAppend[this.Node.id];
        if (h) {
            p.y += h;
        }

        this.Bottom = p;
        nodePositions[this.Node.id] = this;
        return;
    }
    this.init(node);
};
